package com.mmlc.businessutils;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.OrderButtonBean;
import com.mmlc.meimei.lichengkongjian.dialog.CustomDialog;
import com.mmlc.network.filter.CancelOrderRequestFilter;
import com.mmlc.network.responsebean.CancelOrderResponseBean;
import com.mmlc.utils.ToastUtils;

/**
 * Created by Administrator on 2016/8/16 0016.
 */
public class CancelOrderRequest {


    private CustomDialog dialog;

    OrderListUtils orderBean;
    OrderButtonBean buttonBean;


    public CancelOrderRequest(OrderListUtils orderListUtils){
        this.orderBean = orderListUtils;
        buttonBean = orderListUtils.buttonBean;
    }

    public void cancelOrder(){

        View view = showEdit(R.layout.dialog_cancel_order);
        final RadioGroup rg_group = (RadioGroup) view.findViewById(R.id.rg_group);
        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(dialog.dialog.isShowing()){
                    dialog.dialog.dismiss();
                }
                RadioButton checkedView = (RadioButton) group.findViewById(checkedId);
                cancelOrderRequest(buttonBean.orderId,checkedView.getText().toString());
            }
        });
    }

    public View showEdit(int layoutId) {
        if (dialog == null) {
            dialog = new com.mmlc.meimei.lichengkongjian.dialog.CustomDialog(buttonBean.activity);
        }
        dialog.setView(layoutId);
        dialog.show();
        return dialog.getContentView();
    }
    private void cancelOrderRequest(String orderId,String reason) {
        CancelOrderRequestFilter filter = new CancelOrderRequestFilter(buttonBean.activity);
        filter.requestBean.method ="cancelOrder";
        filter.requestBean.order_id =orderId;
        filter.requestBean.reason =reason;
        filter.offerErrorParams(buttonBean.rootView);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<CancelOrderResponseBean>() {

            @Override
            public void onSuccess(CancelOrderResponseBean result) {
                ToastUtils.showToast("订单取消成功");

                if(orderBean.u!= null){
                    if(buttonBean.from == OrderButtonBean.ORDER_DETAIL){
                        orderBean.u.updateUI(buttonBean.itemPositon, OrderListUtils.UpdateUI.FINISH);
                    }else{
                        orderBean.u.updateUI(buttonBean.itemPositon, OrderListUtils.UpdateUI.REMOVE_ITEM);
                    }

                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast("订单取消失败");
            }
        });
    }
}

